package com.yf.service.impl;

import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.yf.dao.AccountDao;
import com.yf.entity.AccountEntity;
import com.yf.service.AccountService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 账户Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Aug 16 13:36:43 CST 2018
 */
@Service
public class AccountServiceImpl extends BaseServiceImpl<AccountEntity> implements AccountService {
    @Autowired
    private AccountDao accountDao;

    /**内部方法**/

    private AccountEntity initAccount(AccountEntity entity){
        entity.setNumber(new BigDecimal(0.0));
        return entity;
    }

    /**接口实现**/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountEntity findUserAccount(Long userId,Integer type) {
        AccountEntity accountEntity = accountDao.findByUserIdAndType(userId,type);
        if(accountEntity!=null){
            return accountEntity;
        }
        accountEntity = new AccountEntity(userId,type);
        initAccount(accountEntity);
        accountDao.save(accountEntity);
        return accountEntity;
    }
}
